package com.douqu.game.core.entity.ext.data.boon;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.config.TimeLimitedActivitiesConfig;
import com.douqu.game.core.entity.ext.data.BaseData;
import com.douqu.game.core.factory.DataFactory;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author wangzhenfei
 *         2017-10-28 16:14
 *         限时活动实体结构
 */
public class TimeLimitedData extends BaseData {

    public int activityId;
    private List<Integer> recordList = new CopyOnWriteArrayList<>();

    public TimeLimitedData() {
    }

    public TimeLimitedData(int activityId) {
        this.activityId = activityId;
    }


    @Override
    public void init() {

    }

    @Override
    public void checkInit() {

    }

    @Override
    public void reset()
    {

    }

    @Override
    public void checkReset() {

    }

    @Override
    public void writeTo(ByteBuffer buffer) {
        buffer.writeInt(activityId);
        buffer.writeShort(recordList.size());
        for(Integer integer : recordList){
            buffer.writeInt(integer);
        }
        buffer.writeBoolean(false);
    }

    @Override
    public void loadFrom(ByteBuffer buffer) {
        activityId = buffer.readInt();
        int size = buffer.readShort();
        for(int i = 0; i < size; i++){
            recordList.add(buffer.readInt());
        }
        buffer.readBoolean();
    }

    public void  addRecord(int recordId){
        if(!containsRecord(recordId)){
            recordList.add(recordId);
        }
    }

    public boolean containsRecord(int recordId){
        return recordList.contains(recordId);
    }

    public List<Integer> getRecordList() {
        return recordList;
    }

    public TimeLimitedActivitiesConfig getConfig(){
        return DataFactory.getInstance().getGameObject(DataFactory.TIME_LIMIT_ACTIVITY_KEY, activityId);
    }

}
